package com.yearEndWorkload.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.listen.ExcelResProp;
import com.yearEndWorkload.listen.PerformanceAppraisalListener;
import com.yearEndWorkload.pojo.excel.importTemplate.PerformanceAppraisalExcel;
import com.yearEndWorkload.pojo.form.add.AddPerformanceAppraisalForm;
import com.yearEndWorkload.pojo.form.update.UpdatePerformanceAppraisalForm;
import com.yearEndWorkload.pojo.query.PerformanceAppraisalQuery;
import com.yearEndWorkload.service.PerformanceAppraisalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/s3/performanceAppraisal")
@Tag(name = "考核平均绩效管理", description = "考核平均绩效管理")
//TODO 已有生成的serviceImpl
public class PerformanceAppraisalController {
    @Autowired
    PerformanceAppraisalService performanceAppraisalService;
    @Autowired
    PerformanceAppraisalListener performancesListener;


    @Operation(summary = "绩效评定信息查询获取", description = "列表查询获取")
    @PostMapping("/getList")
    public Result getList(@RequestBody PerformanceAppraisalQuery performanceAppraisalQuery) {
        return performanceAppraisalService.getList(performanceAppraisalQuery);
    }

    @Operation(summary = "添加一个绩效评定信息", description = "添加一个绩效评定信息")
    @PostMapping("/addPerformanceAppraisal")
    public Result addPerformanceAppraisal(@RequestBody AddPerformanceAppraisalForm performanceAppraisalForm) {
        return performanceAppraisalService.addOne(performanceAppraisalForm);
    }
    @Operation(summary = "更改一个绩效评定信息", description = "更改一个绩效评定信息")
    @PostMapping("/updatePerformanceAppraisal")
    public Result updatePerformanceAppraisal(@RequestBody UpdatePerformanceAppraisalForm updatePerformanceAppraisalForm) {
        return performanceAppraisalService.updateOne(updatePerformanceAppraisalForm);
    }
    @Operation(summary = "删除绩效评定信息", description = "删除绩效评定信息")
    @DeleteMapping("/deletePerformanceAppraisal")
    public Result deletePerformanceAppraisal(@RequestBody List<String> list) {
        return performanceAppraisalService.deleteByIds(list);
    }

    @Operation(summary = "Excel批量导入绩效评定信息", description = "Excel批量导入绩效评定信息")
    @PostMapping("/addByBatch")
    public Result addAByBatch(@RequestParam("file") @Parameter(description = "添加Excel文件") MultipartFile file) throws IOException{

        EasyExcel.read(file.getInputStream(), PerformanceAppraisalExcel.class, performancesListener).sheet().doRead();
        Map<String, Object> map =new HashMap<>();
        map.put("record",performancesListener.getRecord());
        return Result.success(map);

    }

    @Operation(summary = "Excel导出绩效评定信息",description = "Excel导出绩效评定信息")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response) {
        ExcelResProp.setExcelRespProp(response, "绩效评定信息");
        List<PerformanceAppraisalExcel> performanceAppraisalExcel = performanceAppraisalService.getPerformanceAppraisalExcel();
        EasyExcel.write(response.getOutputStream())
                .head(PerformanceAppraisalExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("绩效评定信息")
                .doWrite(performanceAppraisalExcel);
    }


}
